/**
 * Created by gongxun on 17/8/15.
 */

var KwxWxShopLayer = KwxBaseLayer.extend({
    Panel_center: null,
    PageView_list: null,
    Image_fenyes: null,
    //商品列表
    mShopInfos: null,
    //控件列表
    mItems: null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxWxStoreLayer.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");

        this.PageView_list = mo.seekWidgetByName(this.ccsNode, "PageView_list");
        //分页
        this.Image_fenyes = [];
        var Panel_fenye = mo.seekWidgetByName(this.ccsNode, "Panel_fenye");
        for (var i = 0; i < 6; i++) {
            var Image_fenye = mo.seekWidgetByName(Panel_fenye, "Image_fenye" + i);
            this.Image_fenyes.push(Image_fenye);
            Image_fenye.setVisible(false);
        }

        this.PageView_list.addEventListener(function (sender, type) {
            this.updateIndex();
        }, this);

        //关闭
        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.closeThis();
            }
        }, this);

        //关闭
        mo.seekWidgetByName(this.ccsNode, "Panel_touch").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.closeThis();
            }
        }, this);

        //修复剪切bug
        mo.gameHelper.setClippingLayout(this.PageView_list);
    },

    onEnter: function () {
        this._super();
        mo.soundHelper.playDialogOpen();
        this.showAction(this.Panel_center);

        if (cc.sys.isNative) {
            var that = this;
            mo.httpClient.getWechatShopList(function (isSuccess, infos) {
                if (isSuccess) {
                    var infosStr = JSON.stringify(infos);
                    cc.log("infosStr:" + infosStr);

                    infos.push(infos[0]);
                    that.mShopInfos = infos;

                    that.setShopContent();
                } else {
                    cc.log("获取商品列表失败");
                }
            });
        }
    },

    closeThis: function () {
        this.closeAction(this.Panel_center)
    },

    //更新分页小圆点
    updateIndex: function () {
        var count = this.PageView_list.getItems().length;
        var index = this.PageView_list.getCurrentPageIndex();
        var xAdd = 30;
        var startX = -(count - 1) * xAdd / 2;
        for (var i = 0; i < this.Image_fenyes.length; i++) {
            if (i < count) {
                this.Image_fenyes[i].setVisible(true);
                this.Image_fenyes[i].loadTexture(index == i ? "res/ccs/hall_second/gift/point0.png" : "res/ccs/hall_second/gift/point1.png");
                this.Image_fenyes[i].setPositionX(startX + xAdd * i);

            } else {
                this.Image_fenyes[i].setVisible(false);
            }
        }
    },

    //设置商品信息
    setShopContent: function () {
        var len = this.mShopInfos.length;
        var childPanel = null;
        var xs = [90, 270, 450];
        var ys = 100;

        this.mItems = [];

        for (var i = 0; i < len; i++) {
            if (i % 3 == 0) {
                childPanel = new ccui.Layout();
                this.PageView_list.addPage(childPanel);
            }

            var item = new KwxWxShopItem(this.mShopInfos[i], this, this.onTouchItem);
            childPanel.addChild(item);

            var col = i % 3;
            item.setPosition(cc.p(xs[col], ys));
            this.mItems.push(item);

            // item.setTag(i);
            // item.setItemInfo(this.mData[i]);
            // item.addTouchEventListener(this.onTouchItem, this);
        }

        this.updateIndex();
    },

    onTouchItem: function (info) {
        cc.log("info:" + JSON.stringify(info));

        mo.showWaitLayer(10);
        mo.httpClient.getPayOrder(info.pay_item_id, function (isSuccess, order) {
            if (isSuccess) {
                mo.closeWaitLayer();
                cc.log("order:" + order);
                //TODO
                mo.nativeHelper.onNativeWxPay(order, 0.01, "http://192.168.0.1", info.name, "127.0.0.1");
            } else {
                mo.closeWaitLayer();
                mo.showRoomMsgShow("获取订单失败");
            }
        });
    }
});